package com.android.lovingpet.community.sift;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.lovingpet.R;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.GuideAdapter;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.common.postlist.PostListAdapter;
import com.android.lovingpet.common.teaminfo.TeamInfoActivity;
import com.android.lovingpet.common.userinfo.UserInfoActivity;
import com.android.lovingpet.community.posting.AlbumAdapter;
import com.android.lovingpet.community.posting.PostingActivity;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.List;

public class SiftAdapter extends RecyclerView.Adapter {

    private static final int TYPE_PAGER = 0;
    private static final int TYPE_ITEM = 1;
    private Context context;
    private List<Post> posts;
    private View.OnClickListener onClickListener;

    public SiftAdapter(List<Post> posts, View.OnClickListener onClickListener) {
        this.posts = posts;
        this.onClickListener = onClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == TYPE_PAGER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_sift, parent, false);
            view.findViewById(R.id.ll_category).setOnClickListener(onClickListener);
            view.findViewById(R.id.ll_teams).setOnClickListener(onClickListener);
            return new ViewHolder(view);
        }

        View view = LayoutInflater.from(context).inflate(R.layout.item_post, parent, false);
        PostListAdapter.ViewHolder holder = new PostListAdapter.ViewHolder(view);
        view.setOnClickListener(v -> {
            int position = holder.getAdapterPosition() - 1;
            toInfo(position, false);
        });
        view.findViewById(R.id.iv_comment).setOnClickListener(v -> {
            int position = holder.getAdapterPosition() - 1;
            toInfo(position, true);
        });
        view.findViewById(R.id.iv_head).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition() - 1;
                Post post = posts.get(position);
                if (post.getUserType() == 1) {
                    Account account = Utility.getAccount(post.getUserId());
                    if (account != null) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("account", account);
                        context.startActivity(intent);
                    }
                } else {
                    Team team = Utility.getTeam(post.getUserId());
                    if (team != null) {
                        Intent intent = new Intent(context, TeamInfoActivity.class);
                        intent.putExtra("team", team);
                        context.startActivity(intent);
                    }
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder sHolder = (ViewHolder) holder;
            List<Integer> images = new ArrayList<>(3);
            images.add(R.drawable.news1);
            //images.add(R.drawable.news2);
            images.add(R.drawable.news3);
            sHolder.vpNews.setAdapter(new GuideAdapter(images, context));
        }

        if (holder instanceof PostListAdapter.ViewHolder) {
            PostListAdapter.ViewHolder pHolder = (PostListAdapter.ViewHolder) holder;
            Post post = posts.get(position - 1);
            if (post.getUserType() == 1) {
                Account account = Utility.getAccount(post.getUserId());
                if (account != null) {
                    Glide.with(context).load(account.getHead()).into(pHolder.ivHead);
                    pHolder.tvName.setText(account.getName());
                }
            } else {
                Team team = Utility.getTeam(post.getUserId());
                if (team != null) {
                    Glide.with(context).load(team.getImage()).into(pHolder.ivHead);
                    pHolder.tvName.setText(team.getName());
                }
            }
            pHolder.tvTime.setText(post.getCreateTime());
            pHolder.tvContext.setText(post.getContext());
            if (pHolder.recyclerView.getAdapter() == null) {
                pHolder.recyclerView.setAdapter(new AlbumAdapter(post.getImages()));
            } else {
                ((AlbumAdapter) pHolder.recyclerView.getAdapter()).setImages(post.getImages());
            }
            pHolder.recyclerView.getAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return posts.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_PAGER;
        return TYPE_ITEM;
    }

    private void toInfo(int position, boolean isComment) {
        Intent intent = new Intent(context, PostingActivity.class);
        intent.putExtra("post", posts.get(position));
        intent.putExtra("isComment", isComment);
        context.startActivity(intent);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RollPagerView vpNews;

        public ViewHolder(View itemView) {
            super(itemView);
            vpNews = itemView.findViewById(R.id.vp_news);
            vpNews.setPlayDelay(2000);
            vpNews.setHintView(new ColorPointHintView(itemView.getContext(), R.color.colorAccent, Color.WHITE));
        }
    }
}
